package com.studyyoun.android.androidcommonbaselibrary.imageloader;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.studyyoun.android.androidcommonbaselibrary.R;

/**
 * Created by androidlongs on 17/6/29.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

public class ImageLoaderManager {


    /**
     * 默认的参数配制
     */
    //加载图片的启用的线程个娄
    private static final int THREAD_COUNT = 4;
    //优先级
    private static final int PRIORITY = 2;
    //缓存大小
    private static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;
    //连接超时时间
    private static final int CONNECTION_TIME_OUT = 5000;
    //读取超时时间
    private static final int READ_TIME_OUT = 50 * 1000;
    private static ImageLoader mImageLoader = null;
    private static ImageLoaderManager sImageLoaderManager;


    public static ImageLoaderManager getInstance (Context context) {
        if (sImageLoaderManager == null) {
            synchronized (ImageLoaderManager.class) {
                if (sImageLoaderManager == null) {
                    sImageLoaderManager = new ImageLoaderManager(context);
                }
            }
        }
        return sImageLoaderManager;
    }

    private ImageLoaderManager (Context context) {

        ImageLoaderConfiguration configuration = new Builder(context)
                .threadPoolSize(THREAD_COUNT)//配制图片最大下载线程数
                .threadPriority(Thread.NORM_PRIORITY - PRIORITY)//配制优先级
                .denyCacheImageMultipleSizesInMemory()//防止缓存多尺寸的图片
                .memoryCache(new WeakMemoryCache())//使用弱引用
                .diskCacheSize(DISK_CACHE_SIZE)//设置硬盘缓存大小
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//使用MD5命名模式
                .tasksProcessingOrder(QueueProcessingType.LIFO)//图片下载顺序
                .defaultDisplayImageOptions(getDefultOptions())//配制
                .imageDownloader(new BaseImageDownloader(context, CONNECTION_TIME_OUT, READ_TIME_OUT))//设置图片下载器
                .writeDebugLogs()//dubug环境下输出日志
                .build();


        ImageLoader.getInstance().init(configuration);
        mImageLoader = ImageLoader.getInstance();
    }

    /**
     * 图片加载的 Options
     */
    private DisplayImageOptions getDefultOptions () {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.loading_err)//图片地二为空时显示的图片
                .showImageOnFail(R.drawable.loading_err)//图片加载失败显示的图片
                .cacheInMemory(false)//设置图片不可以缓存在内存中
                .cacheOnDisk(true)//设置图片可以缓存在硬盘中
                .bitmapConfig(Config.RGB_565)//设置图片的解码类型
                .decodingOptions(new BitmapFactory.Options())//图片解码配制
                .build();
        return options;
    }


    //---------------------------加载图片------------------------------------------------

    public void displayImage (ImageView imageView, String url, DisplayImageOptions options, ImageLoadingListener loadingListener) {
        if (loadingListener != null) {
            mImageLoader.displayImage(url, imageView, options, loadingListener);
        }
    }

    public void displayImage (ImageView imageView, String url) {
        displayImage(imageView, url, null, null);
    }

    public void displayImage (ImageView imageView, String url, ImageLoadingListener loadingListener) {
        displayImage(imageView, url, null, loadingListener);
    }
}
